<?php

namespace AlbumRest\Controller;

use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractRestfulController;
use AlbumRest\Model\RestTable;
use Album\Model\AlbumTable;
use Zend\View\Model\JsonModel;

class AlbumRestController extends AbstractRestfulController
{
    protected $albumTable;

    public function getList()
    {
        $results = $this->getAlbumTable()->fetchAll();
        $data = array();
        foreach($results as $result) {
            $data[] = $result;
        }
        //print_r($data);
        //exit;
        return new JsonModel(array('data' => $data));
    }

    public function get($id)
    {
        $album = $this->getAlbumTable()->getAlbum($id);
        return new JsonModel(array('data' => $album));
    }

    public function create()
    {

    }

    public function update($id)
    {

    }

    public function delete($id)
    {
        $this->getAlbumTable()->deleteAlbum($id);

        return new JsonModel(array(
            'data' => 'deleted',
        ));
    }


    public function getAlbumTable()
    {
        if(!$this->albumTable)
        {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }

}

